﻿using System;
using BLL;
using DAL;
using Ext.Net;
using System.Data;
using Roles;

namespace ERP.Modules
{
    public partial class Applicants : System.Web.UI.UserControl
    {
        #region[Define]
        private static bool _flagProvince;
        private static bool _flag;
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetApplicants();
                GetPlan();
                GetJob();
                GetNational();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }
        //Store
        protected void StoreApplicants_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            GetApplicants();
        }
        protected void StoreApplicantsEthnic_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchApplicantsEthnic.Text = string.Empty;
            GetEthnic();
        }
        protected void StoreApplicantsDistricts_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchApplicantsDistricts.Text = string.Empty;
            GetDistricts();
        }
        protected void StoreApplicantsReligion_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchReligion.Text = string.Empty;
            GetReligion();
        }
        protected void StoreApplicantsSector_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchSector.Text = string.Empty;
            GetSector();
        }
        protected void StoreApplicantsEducational_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchEducational.Text = string.Empty;
            GetEducational();
        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            _flag = true;
            ClearText();
            WindowsApplicants.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grApplicants.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    _flag = false;
                    ClearText();
                    GetApplicantsEdit(int.Parse(row.RecordID));
                    WindowsApplicants.Show();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grApplicants.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        var obj = new EtApplicants { Applicants_ID = int.Parse(row.RecordID) };
                        BllApplicants.Delete(obj);
                    }
                    catch (Exception ex)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                    }
                }
            GetApplicants();
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsApplicants.Close();
        }
        protected void btnReloadCode_Click(object sender, DirectEventArgs e)
        {
            SelectBoxRecruitmentPlanMain.Text = string.Empty;
            SelectBoxRecruitmentPlanMainDetail.Text = string.Empty;
            GetPlan();
        }
        protected void btnReloadCodeDetail_Click(object sender, DirectEventArgs e)
        {
            SelectBoxRecruitmentPlanMainDetail.Text = string.Empty;
            GetPlanDetail();
        }
        protected void btnReloadJob_Click(object sender, DirectEventArgs e)
        {
            SelectBoxJob.Text = string.Empty;
            GetJob();
        }
        protected void linkEthnic_Click(object sender, DirectEventArgs e)
        {
            GetEthnic();
            WindowApplicantsEthnic.Show();
        }
        protected void btnReligion_Click(object sender, DirectEventArgs e)
        {
            GetReligion();
            WindowReligion.Show();
        }
        protected void btnProvince_Staying_Click(object sender, DirectEventArgs e)
        {
            _flagProvince = false;
            txtDistricts_ID_Staying.Text = string.Empty;
            txtDistricts_Staying.Text = string.Empty;
            txtProvince_Staying.Text = string.Empty;
            grApplicantsDistricts.GetStore().RemoveAll();
            SelectBoxApplicantsNational.Text = string.Empty;
            SelectBoxApplicantsDomain.Text = string.Empty;
            SelectBoxProvincesApplicants.Text = string.Empty;
            GetDistricts();
            WindowApplicantsDistricts.Show();
        }
        protected void btnProvince_Click(object sender, DirectEventArgs e)
        {
            _flagProvince = true;
            txtDistricts_ID.Text = string.Empty;
            txtDistricts.Text = string.Empty;
            txtProvince.Text = string.Empty;
            grApplicantsDistricts.GetStore().RemoveAll();
            SelectBoxApplicantsNational.Text = string.Empty;
            SelectBoxApplicantsDomain.Text = string.Empty;
            SelectBoxProvincesApplicants.Text = string.Empty;
            GetDistricts();
            WindowApplicantsDistricts.Show();
        }
        protected void btnEducational_Click(object sender, DirectEventArgs e)
        {
            GetEducational();
            WindowEducational.Show();
        }
        protected void btnSector_Click(object sender, DirectEventArgs e)
        {
            GetSector();
            WindowSector.Show();
        }
        //Db-Click
        protected void ApplicantsDblClick(object sender, DirectEventArgs e)
        {
            _flag = false;
            ClearText();
            int Applicants_ID = int.Parse(e.ExtraParams["Applicants_ID"]);
            GetApplicantsEdit(Applicants_ID);
            WindowsApplicants.Show();
        }
        //Cell-Click
        protected void CellApplicantsEthnic_Click(object sender, DirectEventArgs e)
        {
            var sm = grApplicantsEthnic.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetEthnicDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowApplicantsEthnic.Close();
        }
        protected void CellApplicantsDistricts_Click(object sender, DirectEventArgs e)
        {
            if (_flagProvince)
            {
                txtDistricts_ID.Text = string.Empty;
                txtDistricts.Text = string.Empty;
                txtProvince.Text = string.Empty;
                txtProvince_ID.Text = string.Empty;
            }
            else
            {
                txtDistricts_ID_Staying.Text = string.Empty;
                txtDistricts_Staying.Text = string.Empty;
                txtProvince_ID_Staying.Text = string.Empty;
                txtProvince_Staying.Text = string.Empty;
            }
            var sm = grApplicantsDistricts.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetDistrictsDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowApplicantsDistricts.Close();
        }
        protected void CellApplicantsReligion_Click(object sender, DirectEventArgs e)
        {
            var sm = grReligion.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetReligionDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowReligion.Close();
        }
        protected void CellApplicantsSector_Click(object sender, DirectEventArgs e)
        {
            var sm = grSector.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetSectorDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowSector.Close();
        }
        protected void CellApplicantsEducational_Click(object sender, DirectEventArgs e)
        {
            var sm = grEducational.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetEducationalDetail(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowEducational.Close();
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var _clsGetApplicants = new BllApplicants();
            var _dtGetApplicants = _clsGetApplicants.GetApplicants(txtSearch.Text);
            if (_dtGetApplicants != null && _dtGetApplicants.Rows.Count > 0)
            {
                StoreApplicants.DataSource = _dtGetApplicants;
                StoreApplicants.DataBind();
            }
            else
            {
                grApplicants.GetStore().RemoveAll();
            }
        }
        protected void SelectBoxRecruitmentPlanMain_Change(object sender, DirectEventArgs e)
        {
            GetPlanDetail();
        }
        protected void ApplicantsEthnic_Event(object sender, DirectEventArgs e)
        {
            var _clsGetEthnic = new Ethnic_BLL();
            var _dtGetEthnic = _clsGetEthnic.BindGirdtb_Ethnic(txtSearchApplicantsEthnic.Text);
            if (_dtGetEthnic != null && _dtGetEthnic.Rows.Count > 0)
            {
                StoreApplicantsEthnic.DataSource = _dtGetEthnic;
                StoreApplicantsEthnic.DataBind();
            }
            else
            {
                grApplicantsEthnic.GetStore().RemoveAll();
            }
        }
        protected void ApplicantsDistricts_Event(object sender, DirectEventArgs e)
        {
            var _clsDistricts = new Districts_BLL();
            var dtSearchDistricts = _clsDistricts.BindGirdDistricts(txtSearchApplicantsDistricts.Text);
            if (dtSearchDistricts != null && dtSearchDistricts.Rows.Count > 0)
            {
                StoreApplicantsDistricts.DataSource = dtSearchDistricts;
                StoreApplicantsDistricts.DataBind();
            }
            else
            {
                grApplicantsDistricts.GetStore().RemoveAll();
            }
        }
        protected void ApplicantsReligion_Event(object sender, DirectEventArgs e)
        {
            var _clsGetReligion = new BllReligion();
            var _dtGetReligion = _clsGetReligion.GetReligion(txtSearchReligion.Text);
            if (_dtGetReligion != null && _dtGetReligion.Rows.Count > 0)
            {
                StoreReligion.DataSource = _dtGetReligion;
                StoreReligion.DataBind();
            }
            else
            {
                grReligion.GetStore().RemoveAll();
            }
        }
        protected void ApplicantsSector_Event(object sender, DirectEventArgs e)
        {
            var _clsGetSector = new Sector_BLL();
            var _dtGetSector = _clsGetSector.BindGirdSector(txtSearchSector.Text);
            if (_dtGetSector != null && _dtGetSector.Rows.Count > 0)
            {
                StoreSector.DataSource = _dtGetSector;
                StoreSector.DataBind();
            }
            else
            {
                grSector.GetStore().RemoveAll();
            }
        }
        protected void ApplicantsEducational_Event(object sender, DirectEventArgs e)
        {
            var _clsGetEducational = new BllEducational();
            var _dtGetEducational = _clsGetEducational.GetEducational(txtSearchEducational.Text);
            if (_dtGetEducational != null && _dtGetEducational.Rows.Count > 0)
            {
                StoreEducational.DataSource = _dtGetEducational;
                StoreEducational.DataBind();
            }
            else
            {
                grEducational.GetStore().RemoveAll();
            }
        }
        //Select-Box
        protected void SelectBoxApplicantsNational_Change(object sender, DirectEventArgs e)
        {
            SelectBoxApplicantsDomain.Text = string.Empty;
            SelectBoxProvincesApplicants.Text = string.Empty;
            grApplicantsDistricts.GetStore().RemoveAll();
            GetDomain();
        }
        protected void SelectBoxApplicantsDomain_Change(object sender, DirectEventArgs e)
        {
            SelectBoxProvincesApplicants.Text = string.Empty;
            grApplicantsDistricts.GetStore().RemoveAll();
            GetProvince();
        }
        protected void SelectBoxProvincesApplicants_Change(object sender, DirectEventArgs e)
        {
            GetDistricts();
        }
        #endregion

        #region[Method]
        private void GetApplicants()
        {
            var _clsGetApplicants = new BllApplicants();
            var _dtGetApplicants = _clsGetApplicants.GetApplicants(string.Empty);
            if (_dtGetApplicants != null && _dtGetApplicants.Rows.Count > 0)
            {
                StoreApplicants.DataSource = _dtGetApplicants;
                StoreApplicants.DataBind();
            }
            else
            {
                grApplicants.GetStore().RemoveAll();
            }
        }
        private void GetApplicantsEdit(int id)
        {
            var _clsGetApplicants = new BllApplicants();
            var _dtGetApplicants = _clsGetApplicants.GetApplicantsEdit(id);
            if (_dtGetApplicants != null && _dtGetApplicants.Rows.Count > 0)
            {
                lbID.Text = _dtGetApplicants.Rows[0]["Applicants_ID"].ToString();
                txtApplicants_No.Text = _dtGetApplicants.Rows[0]["Applicants_No"].ToString();
                txtApplicants_Name.Text = _dtGetApplicants.Rows[0]["Applicants_Name"].ToString();
                txtApplicants_Identification.Text = _dtGetApplicants.Rows[0]["Applicants_Identification"].ToString();
                txtApplicants_PlaceOfIssue.Text = _dtGetApplicants.Rows[0]["Applicants_PlaceOfIssue"].ToString();
                txtApplicants_BirthDay.Text = _dtGetApplicants.Rows[0]["Applicants_BirthDay"].ToString();
                if (_dtGetApplicants.Rows[0]["Applicants_Gender"].ToString() == "Nam")
                {
                    chkApplicants_Gender_Male.Checked = true;
                }
                if (_dtGetApplicants.Rows[0]["Applicants_Gender"].ToString() == "Nữ")
                {
                    chkApplicants_Gender_FeMale.Checked = true;
                }
                txtEthnic.Text = _dtGetApplicants.Rows[0]["Ethnic_Name"].ToString();
                txtEthnic_ID.Text = _dtGetApplicants.Rows[0]["Ethnic_ID"].ToString();
                txtReligion.Text = _dtGetApplicants.Rows[0]["Religion_Name"].ToString();
                txtReligion_ID.Text = _dtGetApplicants.Rows[0]["Religion_ID"].ToString();
                txtApplicants_Address.Text = _dtGetApplicants.Rows[0]["Applicants_Address"].ToString();
                txtDistricts.Text = _dtGetApplicants.Rows[0]["Districts_Name"].ToString();
                txtDistricts_ID.Text = _dtGetApplicants.Rows[0]["Districts_Id"].ToString();
                txtProvince.Text = _dtGetApplicants.Rows[0]["Province_Name"].ToString();
                txtProvince_ID.Text = _dtGetApplicants.Rows[0]["Province_Id"].ToString();
                txtApplicants_Address_Staying.Text = _dtGetApplicants.Rows[0]["Applicants_Address_Staying"].ToString();
                txtDistricts_Staying.Text = _dtGetApplicants.Rows[0]["Districts_Name_Staying"].ToString();
                txtDistricts_ID_Staying.Text = _dtGetApplicants.Rows[0]["Districts_Id_Staying"].ToString();
                txtProvince_Staying.Text = _dtGetApplicants.Rows[0]["Province_Name_Staying"].ToString();
                txtProvince_ID_Staying.Text = _dtGetApplicants.Rows[0]["Province_Id_Staying"].ToString();
                txtApplicants_Email.Text = _dtGetApplicants.Rows[0]["Applicants_Email"].ToString();
                txtApplicants_Phone.Text = _dtGetApplicants.Rows[0]["Applicants_Phone"].ToString();
                txtApplicants_Mobie.Text = _dtGetApplicants.Rows[0]["Applicants_Mobie"].ToString();
                txtEducational_Level.Text = _dtGetApplicants.Rows[0]["Educational_Name"].ToString();
                txtEducational_Level_ID.Text = _dtGetApplicants.Rows[0]["Educational_Level_ID"].ToString();
                txtSector.Text = _dtGetApplicants.Rows[0]["Sector_Name"].ToString();
                txtSector_ID.Text = _dtGetApplicants.Rows[0]["Sector_ID"].ToString();
                txtApplicants_Language_Level.Text = _dtGetApplicants.Rows[0]["Applicants_Language_Level"].ToString();
                txtApplicants_Informatics.Text = _dtGetApplicants.Rows[0]["Applicants_Informatics"].ToString();
                txtApplicants_YearsOfExperience.Text = _dtGetApplicants.Rows[0]["Applicants_YearsOfExperience"].ToString();
                txtApplicants_Wage_Current.Text = _dtGetApplicants.Rows[0]["Applicants_Wage_Current"].ToString();
                txtApplicants_Wage_Order.Text = _dtGetApplicants.Rows[0]["Applicants_Wage_Order"].ToString();
                txtApplicants_Skills.Text = _dtGetApplicants.Rows[0]["Applicants_Skills"].ToString();
                txtApplicants_Experience.Text = _dtGetApplicants.Rows[0]["Applicants_Experience"].ToString();
                txtApplicants_Objectives.Text = _dtGetApplicants.Rows[0]["Applicants_Objectives"].ToString();
                txtApplicants_Introduce.Text = _dtGetApplicants.Rows[0]["Applicants_Introduce"].ToString();
                txtApplicants_Notes.Text = _dtGetApplicants.Rows[0]["Applicants_Notes"].ToString();
                SelectBoxRecruitmentPlanMain.SetValue(int.Parse(_dtGetApplicants.Rows[0]["ID_Main"].ToString()));
                SelectBoxRecruitmentPlanMainDetail.SetValue(int.Parse(_dtGetApplicants.Rows[0]["ID_Main_Detail"].ToString()));
                SelectBoxJob.SetValue(int.Parse(_dtGetApplicants.Rows[0]["Job_ID"].ToString()));
            }
        }
        private void GetPlan()
        {
            var _clsGetPlan = new BllRecruitmentPlanMainMain();
            var _dtGetPlan = _clsGetPlan.GetRecruitmentPlanMain(string.Empty);
            if (_dtGetPlan != null && _dtGetPlan.Rows.Count > 0)
            {
                StoreDepartment.DataSource = _dtGetPlan;
                StoreDepartment.DataBind();
            }
            else
            {
                SelectBoxRecruitmentPlanMain.GetStore().RemoveAll();
            }
        }
        private void GetPlanDetail()
        {
            var _clsGetPlanDetail = new BllRecruitmentPlanMainDetail();
            var _dtGetPlanDetail =
                _clsGetPlanDetail.GetRecruitmentPlanMainDetail(int.Parse(SelectBoxRecruitmentPlanMain.SelectedItem.Value));
            if (_dtGetPlanDetail != null && _dtGetPlanDetail.Rows.Count > 0)
            {
                StorePlanMainDetail.DataSource = _dtGetPlanDetail;
                StorePlanMainDetail.DataBind();
            }
            else
            {
                SelectBoxRecruitmentPlanMainDetail.GetStore().RemoveAll();
            }
        }
        private void GetJob()
        {
            var _clsGetJob = new Job_BLL();
            var _dtGetJob = _clsGetJob.BindGirdtb_Job(string.Empty);
            if (_dtGetJob != null && _dtGetJob.Rows.Count > 0)
            {
                StoreJob.DataSource = _dtGetJob;
                StoreJob.DataBind();
            }
            else
            {
                SelectBoxJob.GetStore().RemoveAll();
            }
        }
        private void GetEducational()
        {
            var _clsGetEducational = new BllEducational();
            var _dtGetEducational = _clsGetEducational.GetEducational(string.Empty);
            if (_dtGetEducational != null && _dtGetEducational.Rows.Count > 0)
            {
                StoreEducational.DataSource = _dtGetEducational;
                StoreEducational.DataBind();
            }
            else
            {
                grEducational.GetStore().RemoveAll();
            }
        }
        private void GetEducationalDetail(int id)
        {
            var _clsGetEducational = new BllEducational();
            var _dtGetEducational = _clsGetEducational.GetEducationalEdit(id);
            if (_dtGetEducational != null && _dtGetEducational.Rows.Count > 0)
            {
                txtEducational_Level_ID.Text = _dtGetEducational.Rows[0]["Educational_Level_ID"].ToString();
                txtEducational_Level.Text = _dtGetEducational.Rows[0]["Educational_Name"].ToString();
            }
        }
        private void GetSector()
        {
            var _clsGetSector = new Sector_BLL();
            var _dtGetSector = _clsGetSector.BindGirdSector(string.Empty);
            if (_dtGetSector != null && _dtGetSector.Rows.Count > 0)
            {
                StoreSector.DataSource = _dtGetSector;
                StoreSector.DataBind();
            }
            else
            {
                grSector.GetStore().RemoveAll();
            }
        }
        private void GetSectorDetail(int id)
        {
            var _clsGetSector = new Sector_BLL();
            var _dtGetSector = _clsGetSector.BindGirdSectorDetail(id);
            if (_dtGetSector != null && _dtGetSector.Rows.Count > 0)
            {
                txtSector_ID.Text = _dtGetSector.Rows[0]["Sector_ID"].ToString();
                txtSector.Text = _dtGetSector.Rows[0]["Sector_Name"].ToString();
            }
        }
        private void GetEthnic()
        {
            var _clsGetEthnic = new Ethnic_BLL();
            var _dtGetEthnic = _clsGetEthnic.BindGirdtb_Ethnic(string.Empty);
            if (_dtGetEthnic != null && _dtGetEthnic.Rows.Count > 0)
            {
                StoreApplicantsEthnic.DataSource = _dtGetEthnic;
                StoreApplicantsEthnic.DataBind();
            }
            else
            {
                grApplicantsEthnic.GetStore().RemoveAll();
            }
        }
        private void GetReligion()
        {
            var _clsGetReligion = new BllReligion();
            var _dtGetReligion = _clsGetReligion.GetReligion(string.Empty);
            if (_dtGetReligion != null && _dtGetReligion.Rows.Count > 0)
            {
                StoreReligion.DataSource = _dtGetReligion;
                StoreReligion.DataBind();
            }
            else
            {
                grReligion.GetStore().RemoveAll();
            }
        }
        private void GetEthnicDetail(int id)
        {
            var _clsGetEthnic = new Ethnic_BLL();
            var _dtGetEthnicDetail = _clsGetEthnic.BindGirdtb_EthnicDetail(id);
            if (_dtGetEthnicDetail != null && _dtGetEthnicDetail.Rows.Count > 0)
            {
                txtEthnic_ID.Text = _dtGetEthnicDetail.Rows[0]["Ethnic_ID"].ToString();
                txtEthnic.Text = _dtGetEthnicDetail.Rows[0]["Ethnic_Name"].ToString();
            }
        }
        private void GetReligionDetail(int id)
        {
            var _clsGetReligion = new BllReligion();
            var _dtGetReligion = _clsGetReligion.GetReligionEdit(id);
            if (_dtGetReligion != null && _dtGetReligion.Rows.Count > 0)
            {
                txtReligion_ID.Text = _dtGetReligion.Rows[0]["Religion_ID"].ToString();
                txtReligion.Text = _dtGetReligion.Rows[0]["Religion_Name"].ToString();
            }
        }
        private void GetDistricts()
        {
            var _clsDistricts = new Districts_BLL();
            if (String.IsNullOrEmpty(SelectBoxProvincesApplicants.Text))
            {
                var _dtGetDistricts = _clsDistricts.BindGirdDistricts(txtSearchApplicantsEthnic.Text);
                if (_dtGetDistricts != null && _dtGetDistricts.Rows.Count > 0)
                {
                    StoreApplicantsDistricts.DataSource = _dtGetDistricts;
                    StoreApplicantsDistricts.DataBind();
                }
                else
                {
                    grApplicantsDistricts.GetStore().RemoveAll();
                }
            }
            else
            {
                var _dtGetDistricts =
                    _clsDistricts.GetDistrictsFillter(int.Parse(SelectBoxProvincesApplicants.SelectedItem.Value));
                if (_dtGetDistricts != null && _dtGetDistricts.Rows.Count > 0)
                {
                    StoreApplicantsDistricts.DataSource = _dtGetDistricts;
                    StoreApplicantsDistricts.DataBind();
                }
                else
                {
                    grApplicantsDistricts.GetStore().RemoveAll();
                }
            }
        }
        private void GetDistrictsDetail(int id)
        {
            var _clsDistricts = new Districts_BLL();
            var _dtGetDistrictsDetail = _clsDistricts.GetDistrictsDetail(id);
            if (_dtGetDistrictsDetail != null && _dtGetDistrictsDetail.Rows.Count > 0)
            {
                if (_flagProvince)
                {
                    txtDistricts_ID.Text = _dtGetDistrictsDetail.Rows[0]["Districts_Id"].ToString();
                    txtDistricts.Text = _dtGetDistrictsDetail.Rows[0]["Districts_Name"].ToString();
                    txtProvince.Text = _dtGetDistrictsDetail.Rows[0]["Province_Name"].ToString();
                    txtProvince_ID.Text = _dtGetDistrictsDetail.Rows[0]["Province_Id"].ToString();
                }
                else
                {
                    txtDistricts_ID_Staying.Text = _dtGetDistrictsDetail.Rows[0]["Districts_Id"].ToString();
                    txtDistricts_Staying.Text = _dtGetDistrictsDetail.Rows[0]["Districts_Name"].ToString();
                    txtProvince_Staying.Text = _dtGetDistrictsDetail.Rows[0]["Province_Name"].ToString();
                    txtProvince_ID_Staying.Text = _dtGetDistrictsDetail.Rows[0]["Province_Id"].ToString();
                }
            }
        }
        private void GetNational()
        {
            var _clsNational = new National_BLL();
            var _dtNational = _clsNational.BindGirdNational(string.Empty);
            if (_dtNational != null && _dtNational.Rows.Count > 0)
            {
                StoreApplicantsNational.DataSource = _dtNational;
                StoreApplicantsNational.DataBind();
            }
            else
            {
                SelectBoxApplicantsNational.GetStore().RemoveAll();
            }
        }
        private void GetDomain()
        {
            var _clsDomain = new Domain_BLL();
            var _dtGetDomain = _clsDomain.GetDomainFillter(int.Parse(SelectBoxApplicantsNational.SelectedItem.Value));
            if (_dtGetDomain != null && _dtGetDomain.Rows.Count > 0)
            {
                StoreApplicantsDomain.DataSource = _dtGetDomain;
                StoreApplicantsDomain.DataBind();
            }
            else
            {
                SelectBoxApplicantsDomain.GetStore().RemoveAll();
            }
        }
        private void GetProvince()
        {
            var _clsProvince = new Province_BLL();
            var _dtGetProvince = _clsProvince.GetProvinceFillter(int.Parse(SelectBoxApplicantsDomain.SelectedItem.Value));
            if (_dtGetProvince != null && _dtGetProvince.Rows.Count > 0)
            {
                StoreProvincesApplicants.DataSource = _dtGetProvince;
                StoreProvincesApplicants.DataBind();
            }
            else
            {
                SelectBoxProvincesApplicants.GetStore().RemoveAll();
            }
        }
        private bool CheckInsert()
        {
            bool kiemtra = false;
            var _clsGetApplicants = new BllApplicants();
            var _dtTypeContract = _clsGetApplicants.GetApplicants(string.Empty);
            if (_dtTypeContract != null && _dtTypeContract.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtTypeContract.Rows)
                {
                    if (lbID.Text != dr["Applicants_ID"].ToString())
                    {
                        if (txtApplicants_No.Text == dr["Applicants_No"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Mã ứng viên này đã tồn tại !");
                        }
                    }
                }
            }
            return kiemtra;
        }
        private void Insert()
        {
            if (_flag)
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private void InsertValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtApplicants
                    {
                        ID_Main = int.Parse(SelectBoxRecruitmentPlanMain.SelectedItem.Value),
                        Level_ID = int.Parse(SelectBoxRecruitmentPlanMainDetail.SelectedItem.Value),
                        Job_ID = int.Parse(SelectBoxJob.SelectedItem.Value),
                        Applicants_No = txtApplicants_No.Text,
                        Applicants_Name = txtApplicants_Name.Text,
                        Applicants_Identification = txtApplicants_Identification.Text,
                        Applicants_PlaceOfIssue = txtApplicants_PlaceOfIssue.Text,
                        Applicants_BirthDay = DateTime.Parse(txtApplicants_BirthDay.Text),
                        Applicants_Gender = chkApplicants_Gender_FeMale.Checked ? "Nữ" : "Nam",
                        Ethnic_ID = String.IsNullOrEmpty(txtEthnic_ID.Text) ? (int?)null : int.Parse(txtEthnic_ID.Text),
                        Religion_ID =
                            String.IsNullOrEmpty(txtReligion_ID.Text) ? (int?)null : int.Parse(txtReligion_ID.Text),
                        Applicants_Address = txtApplicants_Address.Text,

                        Districts_ID = String.IsNullOrEmpty(txtDistricts_ID.Text)
                            ? (int?)null
                            : int.Parse(txtDistricts_ID.Text),

                        Applicants_Address_Staying = txtApplicants_Address_Staying.Text,
                        Districts_ID_Staying = String.IsNullOrEmpty(txtDistricts_ID_Staying.Text)
                            ? (int?)null
                            : int.Parse(txtDistricts_ID_Staying.Text),

                        Applicants_Email = txtApplicants_Email.Text,
                        Applicants_Phone = txtApplicants_Phone.Text,
                        Applicants_Mobie = txtApplicants_Mobie.Text,
                        Educational_Level_ID = String.IsNullOrEmpty(txtEducational_Level_ID.Text)
                            ? (int?)null
                            : int.Parse(txtEducational_Level_ID.Text),

                        Sector_ID = String.IsNullOrEmpty(txtSector_ID.Text)
                            ? (int?)null
                            : int.Parse(txtSector_ID.Text),

                        Applicants_Language_Level = txtApplicants_Language_Level.Text,
                        Applicants_Informatics = txtApplicants_Informatics.Text,
                        Applicants_YearsOfExperience = String.IsNullOrEmpty(txtApplicants_YearsOfExperience.Text)
                            ? (int?)null
                            : int.Parse(txtApplicants_YearsOfExperience.Text)
                    };
                    obj.Sector_ID = String.IsNullOrEmpty(txtSector_ID.Text)
                        ? (int?)null
                        : int.Parse(txtSector_ID.Text);

                    obj.Applicants_Wage_Current = String.IsNullOrEmpty(txtApplicants_Wage_Current.Text)
                        ? (int?)null
                        : int.Parse(txtApplicants_Wage_Current.Text);

                    obj.Applicants_Wage_Order = String.IsNullOrEmpty(txtApplicants_Wage_Order.Text)
                        ? (int?)null
                        : int.Parse(txtApplicants_Wage_Order.Text);

                    obj.Applicants_Skills = txtApplicants_Skills.Text;
                    obj.Applicants_Experience = txtApplicants_Experience.Text;
                    obj.Applicants_Objectives = txtApplicants_Objectives.Text;
                    obj.Applicants_Introduce = txtApplicants_Introduce.Text;
                    obj.Applicants_Notes = txtApplicants_Notes.Text;
                    BllApplicants.Insert(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    ClearText();
                    GetApplicants();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtApplicants
                    {
                        Applicants_ID = int.Parse(lbID.Text),
                        ID_Main = int.Parse(SelectBoxRecruitmentPlanMain.SelectedItem.Value),
                        Level_ID = int.Parse(SelectBoxRecruitmentPlanMainDetail.SelectedItem.Value),
                        Job_ID = int.Parse(SelectBoxJob.SelectedItem.Value),
                        Applicants_No = txtApplicants_No.Text,
                        Applicants_Name = txtApplicants_Name.Text,
                        Applicants_Identification = txtApplicants_Identification.Text,
                        Applicants_PlaceOfIssue = txtApplicants_PlaceOfIssue.Text,
                        Applicants_BirthDay = DateTime.Parse(txtApplicants_BirthDay.Text),
                        Applicants_Gender = chkApplicants_Gender_FeMale.Checked ? "Nữ" : "Nam",
                        Ethnic_ID = String.IsNullOrEmpty(txtEthnic_ID.Text) ? (int?)null : int.Parse(txtEthnic_ID.Text),
                        Religion_ID =
                            String.IsNullOrEmpty(txtReligion_ID.Text) ? (int?)null : int.Parse(txtReligion_ID.Text),
                        Applicants_Address = txtApplicants_Address.Text,

                        Districts_ID = String.IsNullOrEmpty(txtDistricts_ID.Text)
                            ? (int?)null
                            : int.Parse(txtDistricts_ID.Text),

                        Applicants_Address_Staying = txtApplicants_Address_Staying.Text,
                        Districts_ID_Staying = String.IsNullOrEmpty(txtDistricts_ID_Staying.Text)
                            ? (int?)null
                            : int.Parse(txtDistricts_ID_Staying.Text),

                        Applicants_Email = txtApplicants_Email.Text,
                        Applicants_Phone = txtApplicants_Phone.Text,
                        Applicants_Mobie = txtApplicants_Mobie.Text,
                        Educational_Level_ID = int.Parse(txtEducational_Level_ID.Text),

                        Sector_ID = String.IsNullOrEmpty(txtSector_ID.Text)
                            ? (int?)null
                            : int.Parse(txtSector_ID.Text),

                        Applicants_Language_Level = txtApplicants_Language_Level.Text,
                        Applicants_Informatics = txtApplicants_Informatics.Text,
                        Applicants_YearsOfExperience = String.IsNullOrEmpty(txtApplicants_YearsOfExperience.Text)
                            ? (int?)null
                            : int.Parse(txtApplicants_YearsOfExperience.Text)
                    };
                    obj.Sector_ID = String.IsNullOrEmpty(txtSector_ID.Text)
                        ? (int?)null
                        : int.Parse(txtSector_ID.Text);

                    obj.Applicants_Wage_Current = String.IsNullOrEmpty(txtApplicants_Wage_Current.Text)
                        ? (int?)null
                        : int.Parse(txtApplicants_Wage_Current.Text);

                    obj.Applicants_Wage_Order = String.IsNullOrEmpty(txtApplicants_Wage_Order.Text)
                        ? (int?)null
                        : int.Parse(txtApplicants_Wage_Order.Text);

                    obj.Applicants_Skills = txtApplicants_Skills.Text;
                    obj.Applicants_Experience = txtApplicants_Experience.Text;
                    obj.Applicants_Objectives = txtApplicants_Objectives.Text;
                    obj.Applicants_Introduce = txtApplicants_Introduce.Text;
                    obj.Applicants_Notes = txtApplicants_Notes.Text;
                    BllApplicants.Update(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                    GetApplicants();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        #endregion

        #region[ClearText]
        private void ClearText()
        {
            FormPanelApplicants.Reset();
            lbID.Text = string.Empty;
            txtApplicants_No.Text = AutoCode();
        }
        #endregion

        #region[AutoCode]
        private string AutoCode()
        {
            int i;
            var _clsGetApplicants = new BllApplicants();
            var dt = _clsGetApplicants.GetApplicants(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["Applicants_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["Applicants_ID"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["Applicants_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "UV_00" + i;
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Applicants, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                btnAddNew.Visible = Is_BtnAdd;
                btnEdit.Visible = Is_BtnEdit;
                PanelApplicants.Visible = Is_BtnView;
                btnDel.Visible = Is_BtnDel;
                btnExport.Visible = Is_BtnPrint;
            }
        }
        private bool CheckPermisstionDobclick()
        {
            bool check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Applicants, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}